package com.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.pojo.Book;
import com.book.pojo.Favorites;
import com.book.pojo.User;
import com.book.service.BookService;
import com.book.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;
    @Autowired
    private FavoritesService favoritesService;

    //获取图书信息
    @RequestMapping("/getBookData")
    public String getBookData(Model model, Integer page, Integer category) {

        //MyBatis plus  分页写法
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();  //queryWrapper 封装查询条件
        queryWrapper.eq("category", category);  //要查的目标 与 传入数据（这里是枚举类型）
        //new Page<>(查询的当前页, 每页显示的记录数)
        IPage<Book> iPage = bookService.page(new Page<>(page, 4), queryWrapper);

        model.addAttribute("bookList", iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent() - 1); //上页的页码=当前页-1
        model.addAttribute("next", iPage.getCurrent() + 1);
        model.addAttribute("cur", iPage.getCurrent()); //当前页
        model.addAttribute("pages", iPage.getPages()); //总页数

        model.addAttribute("category", category);

        return "bookData";  //把数据送到这个页面
    }


    //图书列表页面
    @GetMapping("/bookList/{category}")
    public String bookList(@PathVariable("category") String category, Model model) {
        model.addAttribute("category", category);
        return "books_list";
    }

    //获取图书列表数据
    @RequestMapping("/getBooksListData")
    public String getBooksListData(String category, Integer page, Integer pageSize, Model model) {
        //MyBatis plus  分页写法
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();  //queryWrapper 封装查询条件
        queryWrapper.eq("category", category);  //要查的目标 与 传入数据（这里是枚举类型）
        //new Page<>(查询的当前页, 每页显示的记录数)
        IPage<Book> iPage = bookService.page(new Page<>(page, pageSize), queryWrapper);
        model.addAttribute("bookList", iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent() - 1); //上页的页码=当前页-1
        model.addAttribute("next", iPage.getCurrent() + 1);
        model.addAttribute("cur", iPage.getCurrent()); //当前页
        model.addAttribute("pages", iPage.getPages()); //总页数
        model.addAttribute("pageSize", pageSize); //每页显示的数据数量
        model.addAttribute("category", category);
        return "booksListData";
    }


    //图书详情
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, Model model, HttpSession session) {
        //TODO 登录则返回收藏状态
        User user = (User) session.getAttribute("user");
        if (user != null) {
            Integer userId = user.getId();
            Favorites favorites = favoritesService.findUserFavorites(userId, id);
            //非空，该用户收藏了该书
            if (!StringUtils.isEmpty(favorites)){
                Integer status = favorites.getStatus();
                //非空加上收藏状态
                model.addAttribute("BookStatus", status);
            }
            //该用户未收藏该书，返回0
            else {
                model.addAttribute("BookStatus",0 );
            }
        }
        //TODO 同时查询图书是否被收藏，返回收藏信息
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "details";
    }

    // 根据书名查书籍信息
    @RequestMapping("/getBookByName")
    public String getBookDataByName(Model model, Integer page, String bookName) {
        //MyBatis plus  分页写法
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();  //queryWrapper 封装查询条件
        queryWrapper.like("name", bookName);
        //new Page<>(查询的当前页, 每页显示的记录数)
        IPage<Book> iPage = bookService.page(new Page<>(page, 4), queryWrapper);

        model.addAttribute("bookList", iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent() - 1); //上页的页码=当前页-1
        model.addAttribute("next", iPage.getCurrent() + 1);
        model.addAttribute("cur", iPage.getCurrent()); //当前页
        model.addAttribute("pages", iPage.getPages()); //总页数
        model.addAttribute("category", 1);
        return "bookData";  // 把数据送到这个页面
    }

    /**
     * 用于Radis测试
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/detailRedis/{id}")
    public String detailTest(@PathVariable("id") Integer id, Model model) {
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "details";
    }
}
